package ui.juego.expedientes

import dominio.datos.Hobbie
import dominio.datos.Rasgo
import dominio.individuos.Villano
import org.uqbar.arena.bindings.PropertyAdapter
import org.uqbar.arena.layout.ColumnLayout
import org.uqbar.arena.layout.HorizontalLayout
import org.uqbar.arena.layout.VerticalLayout
import org.uqbar.arena.widgets.Button
import org.uqbar.arena.widgets.Label
import org.uqbar.arena.widgets.List
import org.uqbar.arena.widgets.Panel
import org.uqbar.arena.widgets.Selector
import org.uqbar.arena.widgets.TextBox
import org.uqbar.arena.windows.Dialog
import org.uqbar.arena.windows.WindowOwner
import ui.juego.utilidades.IconUtils

abstract class EdicionVillanoDialog extends Dialog<Villano> {
	
	new(WindowOwner parent, Villano model) {
		super(parent, model)
		iconImage = IconUtils.path
	}

	new(WindowOwner parent, Villano model, String title) {
		super(parent, model)
		this.title = title
		iconImage = IconUtils.path
	}
	
	override protected addActions(Panel actionsPanel) {}
	
	override protected createFormPanel(Panel mainPanel) {}
	
	override createMainTemplate(Panel mainPanel) {
		var Panel contentPanel = new Panel(mainPanel) => [
			layout = new VerticalLayout
		]
		
		crearNombrePanel(contentPanel)
		crearSexoPanel(contentPanel)
		crearCabeceraPanel(contentPanel, "Señas Particulares")[ |
			new EditarRasgoVillanoDialog(this, new EditarRasgoVillano(modelObject)).open
		]
		crearListadoPanel(contentPanel, "rasgos", "dato", Rasgo)
		crearCabeceraPanel(contentPanel, "Hobbies")[ |
			new EditarHobbieVillanoDialog(this, new EditarHobbieVillano(modelObject)).open
		]
		crearListadoPanel(contentPanel, "hobbies", "dato", Hobbie)
		
		new Button(contentPanel) => [
			caption = "Aceptar"
			bindEnabledToProperty("tieneNombreValido")
			onClick [ |
				accept
			]
		]
	}
	
	
	def crearNombrePanel(Panel owner) {
		var Panel nombrePanel = new Panel(owner) => [
			layout = new HorizontalLayout
		]
		
		new Label(nombrePanel).text = "Nombre: "
		new TextBox(nombrePanel) => [
			bindValueToProperty("nombre")
		]
	}

	def crearSexoPanel(Panel owner) {
		var Panel sexoPanel = new Panel(owner) => [
			layout = new HorizontalLayout
		]
		
		new Label(sexoPanel).text = "Sexo:"
		new Selector(sexoPanel) => [
			bindItemsToProperty("sexosPosibles")
			bindValueToProperty("sexo")
		]
	}
	
	def <T> crearCabeceraPanel(Panel owner, String label, ()=>void action) {
		var Panel headPanel = new Panel(owner) => [
			layout = new ColumnLayout(2)
		]
		
		new Label(headPanel).text = label
		new Button(headPanel) => [
			caption = '''Editar «label»'''
			width = 150
			onClick [ |
				action.apply()
			]
		]
	}

	def <T> crearListadoPanel(Panel owner, String modelProperty, String propertyAdapter, Class<T> type) {
		var Panel listPanel = new Panel(owner) => [
			layout = new VerticalLayout
		]
		
		new List<T>(listPanel) => [
			bindItemsToProperty(modelProperty).adapter = new PropertyAdapter(type, propertyAdapter)
			heigth = 50
			width = 220
		]
		
	}
	
}